#include "DaterMessageWindow.h"
#include "DaterCommon.h"
#include "DaterMainMessageCenterWindow.h"
#include "DaterBaseWindowManager.h"

MessageWindow::MessageWindow(int index, RequestMessage* requestMessage, CEGUI::Window* parent):
BaseWindow(parent)
{
	using namespace CEGUI;

	if(!WindowManager::getSingleton().isWindowPresent("MessageWindow"))
	{
		Window* protoType = WindowManager::getSingleton().loadWindowLayout("MessageWindow.layout");
		_root = protoType->clone("MessageWindow" + PropertyHelper::intToString(index));
	}else
	{
		Window* protoType = WindowManager::getSingleton().getWindow("MessageWindow");
		_root = protoType->clone("MessageWindow" + PropertyHelper::intToString(index));
	}
	
	_root->setYPosition(UDim(index * 0.15f, 0));

	_name = _root->getName().c_str();

	_type = "static";

	_requestMessage = requestMessage;

	_root->getChildAtIdx(0)->setText(mbcs_to_utf8(_requestMessage->getMessageTitle().c_str()));
	_root->getChildAtIdx(1)->setText(mbcs_to_utf8(_requestMessage->getMessageDetail().c_str()));

	_parent->addChildWindow(_root);

	subsrciberEvent();
}

MessageWindow::~MessageWindow()
{
	delete _requestMessage;
	CEGUI::WindowManager::getSingleton().destroyWindow(_root);
}

void MessageWindow::showWindow()
{
}

void MessageWindow::hideWindow()
{
}


void MessageWindow::subsrciberEvent()
{
	_root->getChildAtIdx(2)->subscribeEvent(CEGUI::PushButton::EventMouseClick, CEGUI::Event::Subscriber(
		&MessageWindow::handle_accept, this));

	_root->getChildAtIdx(3)->subscribeEvent(CEGUI::PushButton::EventMouseClick, CEGUI::Event::Subscriber(
		&MessageWindow::handle_refuse, this));
}

bool MessageWindow::handle_accept(const CEGUI::EventArgs& args)
{
	_requestMessage->accept();

	/*static_cast<MainMessageCenterWindow*>(BaseWindowManager::getSingleton().getWindow("MessageCenterWindow"))
		->removeRequestMeesage(this);*/

	return true;
}

bool MessageWindow::handle_refuse(const CEGUI::EventArgs& args)
{
	_requestMessage->refuse();

	/*static_cast<MainMessageCenterWindow*>(BaseWindowManager::getSingleton().getWindow("MessageCenterWindow"))
		->removeRequestMeesage(this);*/

	return true;
}

RequestMessage* MessageWindow::getRequestMessage()
{
	return _requestMessage;
}